//
//  uiNodeLayer.cpp
//  smu_test1
//
//  Created by jiayi on 16/9/26.
//
//

#include "uiCommon.hpp"
#include "uiNodeLayer.h"
#include "AppMacros.h"

USING_NS_CC;
USING_NS_CC_EXT;

uiNodeLayer::~uiNodeLayer()
{
    while( !_nodes.empty() ){
        uiNode* node = _nodes.front();
        node->release();
        _nodes.pop();
    }
}

// on "init" you need to initialize your instance
bool uiNodeLayer::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    auto visibleSize = Director::getInstance()->getVisibleSize();
    
    _root = Node::create();
    _root->setAnchorPoint( Vec2( 0.5f, 0.5f ) );
    _root->setPositionY( visibleSize.height / 2 );
    this->addChild( _root );
    
    _resetMaxSize();
    
    uiNodeManager::getInstance().initializeData();
    
    
    // active update function
    scheduleUpdate();
    
    return true;
}

void uiNodeLayer::_resetMaxSize()
{
    _maxSize.x = _maxSize.z = INT_MAX;
    _maxSize.y = _maxSize.w = 0;
}

void uiNodeLayer::update( float )
{
    uiNodeManager::uiUpdateNodes& newdata = uiNodeManager::getInstance().getNewDatas();
    if( newdata.empty() ){
        return;
    }
    
    uiUpdateNode* node = *( newdata.rbegin() );
    node->node->bind( _root );
    node->node->updateData( node->updateType );
    uiFactory< uiUpdateNode >::getInstance().deleteObject( node );
    newdata.popBack();
    
    if( newdata.empty() ){
        // calibrate pos and resize scroll view
        Vector<Node*> children = _root->getChildren();
        for( int i = 0 ; i < children.size(); ++ i ){
            uiNode* cnode = dynamic_cast< uiNode *>( children.at(i) );
            if( cnode ){
                updateSize( cnode );
            }
        }
        if( getParent() ){
            ScrollView* scroll = dynamic_cast< ScrollView* >( getParent() );
            if( scroll ){
                Size s = scroll->getContentSize();
                Vec2 middle( ( _maxSize.x + _maxSize.y ) / 2, ( _maxSize.z + _maxSize.w ) / 2 );
                Vec2 pos = _root->convertToWorldSpaceAR( Vec2::ZERO );
                float deltaY = pos.y - middle.y;
                Action* move = EaseInOut::create( MoveTo::create( 1, Vec2( 0, s.height / 2 + deltaY ) ), 2 );
                _root->runAction( move );
            }
        }
        
        // reset max size
        _resetMaxSize();
    }
}

void uiNodeLayer::updateSize( uiNode* node )
{
    if( !node ){
        return;
    }
    Vec2 worldPos = node->getWorldPosition();
    Size size = node->getContentSize();
    
    _maxSize.x = min( worldPos.x - size.width, _maxSize.x );
    _maxSize.y = max( worldPos.x + size.width, _maxSize.y );
    
    _maxSize.z = min( worldPos.y - size.height, _maxSize.z );
    _maxSize.w = max( worldPos.y + size.height, _maxSize.w );
    
    auto visibleSize = Director::getInstance()->getVisibleSize();
    
    if( getParent() ){
        ScrollView* scroll = dynamic_cast< ScrollView* >( getParent() );
        float width = max( _maxSize.y - _maxSize.x, visibleSize.width );
        float height = max( _maxSize.w - _maxSize.z, visibleSize.height ) ;
        if( scroll ){
            scroll->setContentSize( Size( width, height ) );
        }
    }
}
